/*
 * =====================================================================================
 * Copyright (C) 2024 Ingenic Semiconductor Co.,Ltd
 * All Rights Reserved
 *
 * Filename     : completion.c
 * Author       : Keven <keven.ywhan@ingenic.com>
 * Created      : 2024/05/16 18:23
 * Description  :
 *
 * =====================================================================================
 */

#include "completion.h"

inline void init_completion(struct completion *x)
{
	x->wait = xSemaphoreCreateCounting((UBaseType_t)32, (UBaseType_t)0);
}

inline void reinit_completion(struct completion *x)
{
	vSemaphoreDelete(x->wait);
}


void wait_for_completion(struct completion *x)
{
	xSemaphoreTake(x->wait, portMAX_DELAY);

}

uint32_t wait_for_completion_timeout(struct completion *x, uint32_t timeout)
{
	return xSemaphoreTake(x->wait, (TickType_t)timeout);
}

void complete(struct completion *x)
{
	xSemaphoreGive(x->wait);
}

void complete_isr(struct completion *x)
{
	BaseType_t xHigherPriorityTaskWoken = pdFALSE;
	xSemaphoreGiveFromISR( x->wait, &xHigherPriorityTaskWoken );
}
